/**
 * This file is part of "Subversion Server Configuration (win32)".

    "Subversion Server Configuration (win32)" is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Subversion Server Configuration (win32)" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with "Subversion Server Configuration (win32)".  If not, see <http://www.gnu.org/licenses/>.
 * 
 *
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Subversion_Configuration_Win32
{
    public partial class FORM_settings : Form
    {
        private svn_files savedConfig;
        private FORM_base parent;


        public FORM_settings(FORM_base parent)
        {
            InitializeComponent();
            this.parent = parent;
            savedConfig = new svn_files();
            setUpDefaults();
        }

        private void setUpDefaults()
        {
            this.textBox_svn_config.Text = savedConfig.subversion_CONFIG_FILE;
            this.textBox_svn_acl.Text = savedConfig.subversion_ACL;
            this.textBox_svn_auth.Text = savedConfig.subversion_AUTH_FILE;
            this.textBox_svn_repos.Text = savedConfig.subversion_REPOS_PATH;
            this.textBox_apache_httpd.Text = savedConfig.apache_HTTPD_FILE;
            this.textBox_htpasswd.Text = savedConfig.htpasswd_UTILITY_FILE;
        }

        private string openFile(string fileExt, string desc)
        {
            OpenFileDialog browseFile = new OpenFileDialog();

            
            browseFile.Filter = desc + " Files (*."+ fileExt+")|*."+fileExt+"";

            browseFile.Title = "Browse " + desc+ " files";

            String retVal = "";

            if (browseFile.ShowDialog() == DialogResult.Cancel)
                return null;

            try
            {
                retVal = browseFile.FileName;
            }

            catch (Exception)
            {
                MessageBox.Show("Error opening file",
                    "File Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);

            }

            return retVal;
        }


        private string openDir()
        {
            FolderBrowserDialog browseDir = new FolderBrowserDialog();

            //browseDir.RootFolder
            String retVal = "";

            if (browseDir.ShowDialog() == DialogResult.Cancel)
                return null;           
                
            retVal = browseDir.SelectedPath;            

            return retVal;
        }

        private void button_browse_subversion_conf_Click(object sender, EventArgs e)
        {
            this.textBox_svn_config.Text = openFile("conf", "CONFIG");
        }

        private void button_browse_svn_acl_Click(object sender, EventArgs e)
        {
            this.textBox_svn_acl.Text = openFile("*","ALL");
        }

        private void button_browse_svn_auth_Click(object sender, EventArgs e)
        {
            this.textBox_svn_auth.Text = openFile("*","ALL");
        }

        private void button_browse_svn_repo_path_Click(object sender, EventArgs e)
        {
            this.textBox_svn_repos.Text = openDir();
        }

        private void button_browse_apache_httpd_Click(object sender, EventArgs e)
        {
            this.textBox_apache_httpd.Text = openFile("conf","CONFIG");
        }

        private void button_browse_htpasswd_Click(object sender, EventArgs e)
        {
            this.textBox_htpasswd.Text = openFile("exe", "EXECUTABLE");
        }

        private void button_save_config_Click(object sender, EventArgs e)
        {
            savedConfig.apache_HTTPD_FILE = this.textBox_apache_httpd.Text;
            savedConfig.subversion_CONFIG_FILE = this.textBox_svn_config.Text;
            savedConfig.subversion_ACL = this.textBox_svn_acl.Text;
            savedConfig.subversion_AUTH_FILE = this.textBox_svn_auth.Text;
            savedConfig.subversion_REPOS_PATH = this.textBox_svn_repos.Text;
            savedConfig.htpasswd_UTILITY_FILE = this.textBox_htpasswd.Text;

            savedConfig.Save();
            this.parent.UpdateAll();
            this.Close();

        }



    }
}